﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mogre;

namespace WastedMechanics
{
    public class GuiScrollBar : GuiControl
    {
        public enum ScrollBarOrientation
        {
            Vertical,
            Horizontal
        }
        
        public GuiControl Slider;
        public float ButtonsSize;

        public float Max;
        public float Min;
        public float Position;        
        float LastPosition;

        public ScrollBarOrientation Orientation;

        public event Action<GuiScrollBar> OnScroll;

        public GuiScrollBar(string name)
            : base(name)
        {           
            Slider = new GuiControl(name + "Slider");
            Slider.AttachTo(this);
        }

        protected override void UpdateInternals()
        {
            base.UpdateInternals();

            Position = Helper.Clamp(Min, Max, Position);
            if (LastPosition != Position)
            {
                if (OnScroll != null)
                    OnScroll(this);
                LastPosition = Position;
            }
            float dist = (Position - Min) / (Max- Min);

            if (Orientation == GuiScrollBar.ScrollBarOrientation.Horizontal)
            {
                Slider.Left = dist * (Width - ButtonsSize);
                Slider.Height = Height;
                Slider.Width = ButtonsSize;
                Slider.Top = 0;
            }
            else
            { 
                Slider.Top = dist * (Height - ButtonsSize);
                Slider.Width = Width;
                Slider.Height = ButtonsSize;
                Slider.Left = 0;
            }
        }

        bool IsSliding;

        protected override bool ProcessInput()
        {
            if (base.ProcessInput())
            {
                if (IsSliding)
                {
                    if (Orientation == ScrollBarOrientation.Horizontal)                    
                        Position = Min + (Max - Min) * ((Engine.MouseInput.Position.x - DerivedLeft)) / Width;                    
                    else                      
                        Position = Min + (Max - Min) * ((Engine.MouseInput.Position.y - DerivedTop)) / Height;                    

                    if (!Engine.MouseInput.ButtonDown(MOIS.MouseButtonID.MB_Left))                    
                        IsSliding = false;                    
                }   

                if (ContainsPoint(Engine.MouseInput.Position))
                {                    
                    if (Engine.MouseInput.ButtonDown(MOIS.MouseButtonID.MB_Left))
                    {
                        IsSliding = true;
                        return false;
                    }
                }
                                    
                return true;
            }
            else
                return false;
        }  
    }
}
